import { defineStore } from "pinia";
import { reqLogOut, reqLogin, reqUserInfo } from '@/api/user/index.ts'
import { asyncRoutes, anyRoute, constantRoutes } from '@/router/routes.ts'
import router from '@/router'
//@ts-ignore
import cloneDeep from 'lodash/cloneDeep'
const filterAsyncRoute = (asyncRoutes: any, routes: any) => {
    return asyncRoutes.filter((item: any) => {
      if (routes.includes(item.name)) {
        if (item.children && item.children.length) {
          item.children = filterAsyncRoute(item.children, routes)
        }
        return true
      }
    })
  }

let userInfoStore = defineStore('user', {
    state: () => {
        return {
            token: localStorage.getItem('TOKEN'),
            meau: constantRoutes,
            userName: '',
            avatar: '',
            buttons:[]
        }
    },
    getters: {},
    //异步，逻辑的地方
    actions: {
      
        //用户登录的方法
        async reqLogin(data: any) {

            let result = await reqLogin(data);
            //如果请求成功就返回token，失败就返回错误信息
            if (result.code === 200) {
                this.token = result.data ? result.data : null;
              
                localStorage.setItem('TOKEN', this.token)
                return 'ok'
            } else {
                //new一个错误对象返回出去
                return Promise.reject(new Error(result.data))
            }
        },
        //获取用户信息的方法
        async userInfo() {

            console.log(JSON.parse(JSON.stringify(constantRoutes)))
            let result = await reqUserInfo();
            if (result.code === 200) {
                this.userName = result.data.name
                this.avatar = result.data.avatar
                this.buttons=result.data.buttons
                let userAsync = filterAsyncRoute(cloneDeep(asyncRoutes), result.data.routes)
               
                console.log(anyRoute);
                
                this.meau = [...userAsync, ...constantRoutes, anyRoute]

                console.log("meau======", this.meau);

                //    让路由器管理新的异步路由和任意路由
                const allRoutes = [...userAsync, anyRoute];
             
                console.log(allRoutes);

                allRoutes.forEach(route => {
                    //异步路由动态追加，可能会导致没加载完毕的情况
                    

                    router.addRoute(route)
                })
                return 'ok'
            } else {
                //new一个错误对象返回出去

                return Promise.reject(new Error(result.message))
            }
        },
        //退出登录
        async logOut() {
            let result = await reqLogOut()
            if (result.code === 200) {
                this.userName = ''
                this.avatar = ''
                this.token = '';
                //把持久化去掉
                localStorage.removeItem('TOKEN')
                return 'ok'
            } else {
                //new一个错误对象返回出去

                return Promise.reject(new Error(result.message))
            }

        }
    }

})



export default userInfoStore